package com.model.action;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.RequestAware;

@SuppressWarnings("serial")
public class DownloadAction extends BaseAction implements RequestAware{
 
	   Map request; 
	   private String fileName;
	   private String filePath;
	   private String fileLength;

       public void setFileName(String fileName) {
               this.fileName = fileName;
       }
       public InputStream getExcelStream() {
    	   InputStream inputStream = ServletActionContext.getServletContext().getResourceAsStream("/"+filePath +"/"+ fileName);
    	   if(inputStream != null){
    		   return inputStream;
    	   }else{
    		   return null;
    	   }
       }      
       @SuppressWarnings("unchecked")
	public String execute(){
    	   if(getExcelStream() != null){
    		   return SUCCESS;
    	   }else{
    		   request.put("message", "<script >alert('下载出错');</script>");
    		   return ERROR; 
    	   }
       }
	public String getFileName() {
		try { 
			fileName = new String(fileName.getBytes(), "ISO8859-1");
			} catch (UnsupportedEncodingException e) {  
				
		}
		return fileName;
	}
	public String getFilePath() {
		return filePath;
	}
	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}
	public String getFileLength() {
		try {
		fileLength = ""+ServletActionContext.getServletContext().getResourceAsStream("/"+filePath +"/"+ fileName).available();
		
			return fileLength;
		} catch (IOException e) {
		
		}
	    return fileLength;   
	}
	public void setFileLength(String fileLength) {
		this.fileLength = fileLength;
	}
	public Map getRequest() {
		return request;
	}
	public void setRequest(Map request) {
		this.request = request;
	}
	
       
}
